import { createStore } from 'vuex'
import { getinfo } from "@/api/manager.js";
import { removecookies } from "@/utils/cookies.js";
import { uselogout } from "@/api/manager.js";
import router from "@/router/index.js";

// 创建一个新的 store 实例
const store = createStore({
    state() {
        return {
            userinfo: {},
            asideWidth: '250px',
            menus: [],
            rulesName: ''
        }
    },
    mutations: {
        setuserinfo(state, value) {
            state.userinfo = value
        },
        chanceasideWidth(state){
            state.asideWidth = state.asideWidth === '250px' ? '64px' : '250px'
        },
        chancemenus(state, value) {
            state.menus = value
        },
        chancerulesName(state, value) {
            state.rulesName = value
        }
    },
    getters: {

    },
    actions: {
        actionsSetuserinfo(context) {
            getinfo().then(result => {
                context.commit('setuserinfo', result)
                context.commit('chancemenus', result.menus)
                context.commit('chancerulesName', result.role) 
            }).catch(err => {

            });
        },
        actionsLogout(context) {
            uselogout().finally(() => {
                context.commit('setuserinfo', {});
                removecookies("user-token");
                router.push("/Login");
            });
        }

    },

})

export default store